package adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.sxpjyy.monkey.mobilenurse.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import objects.LisJYJG;
import objects.LisJYXM;

/**
 * Created by Monkey on 2017/4/19.
 * 检验结果适配器
 */

public class JYJGListAdapter_2 extends BaseExpandableListAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private Map<LisJYXM, List<LisJYJG>> dataSets;
    private List<LisJYXM> parentsList;

    public JYJGListAdapter_2(Context context) {
        this.mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setDataSets(Map<LisJYXM, List<LisJYJG>> dataSets) {
        this.dataSets = dataSets;
        getParentList();
        Collections.sort(parentsList, new Comparator<LisJYXM>() {
            public int compare(LisJYXM arg0, LisJYXM arg1) {
                return arg0.getBGDH().compareTo(arg1.getBGDH());
            }
        });
    }

    private void getParentList() {
        Set set = dataSets.keySet();
        parentsList = new ArrayList<>();
        for (Object aSet : set) {
            parentsList.add((LisJYXM) aSet);
        }
    }

    @Override
    public int getGroupCount() {
        if (parentsList != null && parentsList.size() > 0) {
            return parentsList.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (parentsList != null && parentsList.get(groupPosition) != null && dataSets.get(parentsList.get(groupPosition)) != null) {
            return dataSets.get(parentsList.get(groupPosition)).size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (parentsList != null) {
            return parentsList.get(groupPosition);
        }
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (parentsList != null && dataSets.get(parentsList.get(groupPosition)) != null) {
            return dataSets.get(parentsList.get(groupPosition)).get(childPosition);
        }
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder holder;
        if (convertView == null) {
            holder = new ParentViewHolder();
            convertView = inflater.inflate(R.layout.item_jyxm, parent, false);
            holder.jy_name = (TextView) convertView.findViewById(R.id.jy_name);
            holder.sq_date = (TextView) convertView.findViewById(R.id.jy_date_sq);
            holder.jy_date = (TextView) convertView.findViewById(R.id.jy_date_jy);
            holder.jy_doctor = (TextView) convertView.findViewById(R.id.jy_doctor);
            holder.jy_status = (TextView) convertView.findViewById(R.id.jy_status);
            convertView.setTag(holder);
        } else {
            holder = (ParentViewHolder) convertView.getTag();
        }
        if (parentsList != null && parentsList.size() > 0) {
            LisJYXM lisJYXM = parentsList.get(groupPosition);
            if (lisJYXM != null) {
                String jyxm = lisJYXM.getJYXM() == null ? "" : lisJYXM.getJYXM();
                holder.jy_name.setText(jyxm);
                String sqrq = lisJYXM.getSQRQ() == null ? "" : lisJYXM.getSQRQ().substring(0, 10);
                holder.sq_date.setText(sqrq);
                String jyrq = lisJYXM.getJYRQ() == null ? "" : lisJYXM.getJYRQ().substring(0, 10);
                holder.jy_date.setText(jyrq);
                String sqys = lisJYXM.getSQYS() == null ? "" : lisJYXM.getSQYS();
                holder.jy_doctor.setText(sqys);
                if (lisJYXM.getBGDH() != null && !lisJYXM.getBGDH().equals("")) {
                    holder.jy_status.setText("已报告");
                    holder.jy_status.setTextColor(mContext.getResources().getColor(R.color.colorBlue));
                } else {
                    holder.jy_status.setText("未报告");
                    holder.jy_status.setTextColor(mContext.getResources().getColor(R.color.colorRed));
                }
            }
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder holder;
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = inflater.inflate(R.layout.item_jyjg_2, parent, false);
            holder.jg_name = (TextView) convertView.findViewById(R.id.jg_name);
            holder.jg_value = (TextView) convertView.findViewById(R.id.jg_value);
            holder.jg_flag = (TextView) convertView.findViewById(R.id.jg_flag);
            holder.jg_unit = (TextView) convertView.findViewById(R.id.jg_unit);
            holder.jg_reference = (TextView) convertView.findViewById(R.id.jg_reference);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        List<LisJYJG> list = dataSets.get(parentsList.get(groupPosition));
        if (list != null && list.size() > 0) {
            Collections.sort(list, new Comparator<LisJYJG>() {
                public int compare(LisJYJG arg0, LisJYJG arg1) {
                    return arg0.getXMMC().compareTo(arg1.getXMMC());
                }
            });
            LisJYJG lisJYJG = list.get(childPosition);
            if (lisJYJG != null) {
                String XMMC = lisJYJG.getXMMC() != null ? lisJYJG.getXMMC() : "";
                holder.jg_name.setText(XMMC);
                String JG = lisJYJG.getJG() != null ? lisJYJG.getJG() : "";
                holder.jg_value.setText(JG);
                String YCXS = lisJYJG.getYCXS() != null ? lisJYJG.getYCXS() : "";
                holder.jg_flag.setText(YCXS);
                String DW = lisJYJG.getDW() != null ? lisJYJG.getDW() : "";
                holder.jg_unit.setText(DW);
                String CKZ = lisJYJG.getCKZ() != null ? lisJYJG.getCKZ() : "";
                holder.jg_reference.setText(CKZ);
            }
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    private class ParentViewHolder {
        private TextView jy_name;
        private TextView sq_date;
        private TextView jy_date;
        private TextView jy_doctor;
        private TextView jy_status;
    }

    private class ChildViewHolder {
        private TextView jg_name;
        private TextView jg_value;
        private TextView jg_flag;
        private TextView jg_unit;
        private TextView jg_reference;
    }
}
